﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using GestionPedago.Services.ES;
using GestionPedago.Services;
using GestionPedago.Services.ES.Modele;
using System.Collections.ObjectModel;

namespace GestionPedago.ViewModel{
    
    /// <summary>
    /// Classe qui représente l'onglet Recherche d'ECF
    /// </summary>
    public class RechercheECFVM : OngletVM 
    {

        private ICommand rechercherECFCommand;
        public ICommand RechercherECFCommand
        {
            get
            {
                if (this.rechercherECFCommand == null)
                    this.rechercherECFCommand = new RelayCommand(param => this.RechercheECF());

                return this.rechercherECFCommand;
            }
        }


        private ICommand createECFCommand;
        public ICommand CreateECFCommand
        {
            get
            {
                if (this.createECFCommand == null)
                    this.createECFCommand = new RelayCommand(param => this.CreationModificationECF());

                return this.createECFCommand;
            }
        }

        

        /// <summary>
        /// Ouvre l'longlet d'édition des ECF
        /// </summary>
        private void CreationModificationECF()
        {
            CreationModificationECFVM vmCreationModificationECF = new CreationModificationECFVM(Parent);
            Parent.Onglets.Add(vmCreationModificationECF);
            Parent.SelectedOnglet = vmCreationModificationECF;
        }

        /// <summary>
        /// Liste de tous les modules
        /// </summary>
        public List<ModuleES> Modules
        {
            get { return FacadeServices.GetInstance().RecupererModules(new RecupererModulesIn()).ListeModules; }
        }
        /// <summary>
        /// Critère de recherche Module
        /// </summary>
        public ModuleES Module { get; set; }


        /// <summary>
        /// Liste de toutes les compétences
        /// </summary>
        public List<CompetenceES> Competences
        {
            get { return FacadeServices.GetInstance().RecupererCompetences(new RecupererCompetencesIn()).ListeCompetences; }
        }
        /// <summary>
        /// Critère de recherche Competence
        /// </summary>
        public CompetenceES Competence { get; set; }

        /// <summary>
        /// Critère de recherche Nom de l'ECF
        /// </summary>
        public string NomECF { get; set; }

        /// <summary>
        /// Liste des ECF trouvés
        /// </summary>
        public ObservableCollection<ECFES> ECFs { get; set; }


        /// <summary>
        /// Constructeur de la classe RechercheECFVM
        /// </summary>
        /// <param name="parent">Gestionnaire d'onglets</param>
        public RechercheECFVM(GestionnaireOngletsVM parent)
        {
            base.DisplayName = "Rechercher un ECF";
            base.Parent = parent;

            this.NomECF = "";

            ECFs = new ObservableCollection<ECFES>();
        }


        /// <summary>
        /// Lance la recherche des ECF avec les critères sélectionnés
        /// </summary>
        public void RechercheECF()
        {

            int? pIdModule = null, pIdCompetence = null;
            if (this.Module != null)
                pIdModule = this.Module.IdModule;

            if (this.Competence != null)
                pIdCompetence = this.Competence.IdCompetence;

            RechercherListeECFIn criteresRecherche = new RechercherListeECFIn(
                                                            this.NomECF,
                                                            pIdModule,
                                                            pIdCompetence
                                                        );

            ECFs.Clear();

            foreach (ECFES e in FacadeServices.GetInstance().RechercherListeECF(criteresRecherche).listeECF)
                ECFs.Add(e);
        }
    }
}
